package cn.project.lemontree.service.impl;

import cn.project.lemontree.bean.Online_source;
import cn.project.lemontree.bean.Video;
import cn.project.lemontree.service.Online_sourceService;
import cn.project.lemontree.dao.Online_sourceDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service("online_sourceService")
public class Online_sourceServiceimpl extends ServiceImpl<Online_sourceDao, Online_source> implements Online_sourceService {
    @Resource
    private Online_sourceDao online_sourceDao;

    @Override
    public boolean save(MultipartFile file, Online_source online_source){
        String fileurl = saveFile(file);
        online_source.setAddress(fileurl);
        return online_sourceDao.save(online_source) > 0;
    }

    @Override
    public boolean add(Online_source onlineSource) {
        return online_sourceDao.save(onlineSource)>0;
    }

    public String saveFile(MultipartFile file){
        String fileurl = "";
        try{
            String filepath = "D://lemontree/Resources";
            File realpath = new File(filepath);
            if(!realpath.exists()){
                boolean mkdirs = realpath.mkdirs();
            }
            String filename = UUID.randomUUID().toString() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            file.transferTo(new File(realpath + "/" + filename));
            fileurl = filepath + "/" + filename;
        }catch (IOException e) {
            e.printStackTrace();
        }
        return fileurl;
    }
}
